package com.itjcloud.iot.gateway.filter;

import com.itjcloud.iot.gateway.model.CommonTokenHeaderEnum;
import com.itjcloud.iot.gateway.model.CommonTokenInfo;
import org.springframework.cloud.gateway.filter.headers.HttpHeadersFilter;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;


@Component
public class HttpHeadersAuthInfoRequestFilter implements HttpHeadersFilter {
    @Override
    public HttpHeaders filter(HttpHeaders input, ServerWebExchange exchange) {
        CommonTokenInfo commonTokenInfo = exchange.getAttribute(CommonTokenInfo.class.getName());

        if (commonTokenInfo == null) {
            //如果没有认证吧数据重置为空, 防止前端恶意传递
            for (CommonTokenHeaderEnum value : CommonTokenHeaderEnum.values()) {
                input.add(value.getHeaderName(), "");
            }
            return input;
        }
        commonTokenInfo.toMap().forEach((k, v) -> input.add(k, String.valueOf(v)));
        return input;
    }


}
